package com.dreamstations.changshads.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.activity.ArticleDetailActivity;
import com.dreamstations.changshads.activity.ArticleDetail_WebActivity;
import com.dreamstations.changshads.adapter.DSRvAdapter;
import com.dreamstations.changshads.entity.LibraryBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.DSUtils;
import com.dreamstations.changshads.util.RequestType;
import com.dreamstations.changshads.view.Slide_Indicator;
import com.squareup.picasso.Picasso;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 7YHong on 2016/6/20.
 */
public class Library_Header_Frag extends BaseFragmentForSection<LibraryBean> {

    public Library_Header_Frag() {
    }


    @SuppressLint("ValidFragment")
    public Library_Header_Frag(String title, @RequestType String reqType, int category) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("reqType", reqType);
        bundle.putInt("category", category);
        setArguments(bundle);
    }

    @SuppressWarnings("WrongConstant")
    @NonNull
    @Override
    public DSRequest getDsRequest() {
        Bundle bundle = getArguments();
        return new DSRequest()
                .type(bundle.getString("reqType"))
                .addParams("page", String.valueOf(1))
                //二级分类
                .setTag(Library_Header_Frag.this)
                .addParams("typeId", String.valueOf(bundle.getInt("category")))
                .cacheType(DSUtils.NETWORK_ELSE_CACHED);         //缓存读取策略，默认是NETWORK_ELSE_CACHE，可以不设;
    }

    @NonNull
    @Override
    public DSRvAdapter.RvSupport<LibraryBean> getItem() {
        return new DSRvAdapter.RvSupport<LibraryBean>() {

            @Override
            public int getItemViewType(LibraryBean data) {
                return 0;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                return ViewHolder.get(context, null, parent, R.layout.library_information_item, -1);
            }

            @Override
            public void convert(ViewHolder holder, LibraryBean data) {
                holder.setText(R.id.textView7, data.getTitle());
                picasso.load(DSRequest.SERVER_BASE1 + data.getCover())
                        .placeholder(R.drawable.img_loading)
                        .into((ImageView) holder.getView(R.id.item_cover));
                holder.setText(R.id.item_time, data.getTime());
            }
        };
    }

    @Override
    public OnItemClickListener getOnItemClickListener() {
        return new OnItemClickListener<LibraryBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, LibraryBean o, int position) {
                Toast.makeText(getContext(), "onItemClick:" + position + o.getTitle(), Toast.LENGTH_SHORT).show();
                Intent intent;
                if (o.getTemplate() == 1) {
                    intent = new Intent(getActivity(), ArticleDetail_WebActivity.class);
                    intent.putExtra("data", o);
                } else {
                    intent = new Intent(getActivity(), ArticleDetailActivity.class);
                    intent.putExtra("data", o);
                }
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, LibraryBean o, int position) {
                return false;
            }
        };
    }

    Picasso picasso;
    ViewPager viewPager;

    @Override
    public DSRvAdapter.RvSupport getHeader() {
        picasso = Picasso.with(getContext());
        Bundle bundle = getArguments();
        final List<ImageView> imageViews = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ImageView imageView = new ImageView(this.getActivity());
            // 异步加载图片
            picasso.load("http://i1.buimg.com/3ee901bfed3694d3s.png").into(imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageViews.add(imageView);
        }
        new DSRequest()
                .type(RequestType.TYPE_LIBRARY)
                .addParams("page", String.valueOf(1))
                //二级分类
                .addParams("typeId", String.valueOf(bundle.getInt("category")))
                .addParams("front", String.valueOf(1))
                .setTag(Library_Header_Frag.this)
                .cacheType(DSUtils.CACHED_ELSE_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<LibraryBean>>() {
                    @Override
                    public void onResponse(List<LibraryBean> result) {
                        for (int i = 0; i < result.size(); i++) {
                            picasso.load(DSRequest.SERVER_BASE1
                                    + result.get(i).getCover())
                                    .placeholder(R.drawable.img_loading).into(imageViews.get(i));
                            final LibraryBean libraryBean = result.get(i);
                            imageViews.get(i).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent;
                                    if (libraryBean.getTemplate() == 1) {
                                        intent = new Intent(getActivity(), ArticleDetail_WebActivity.class);
                                        intent.putExtra("data", libraryBean);
                                    } else {
                                        intent = new Intent(getActivity(), ArticleDetailActivity.class);
                                        intent.putExtra("data", libraryBean);
                                    }
                                    startActivity(intent);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(getActivity(), "网络错误", Toast.LENGTH_LONG).show();
                    }
                });
        DSRvAdapter.RvSupport header = new DSRvAdapter.RvSupport() {
            @Override
            public int getItemViewType(Object data) {
                return 0;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                ViewHolder holder = ViewHolder.get(context, null, parent, R.layout.simple_vpheader, -1);
                viewPager = holder.getView(R.id.header_viewpager);
                Slide_Indicator indicator = holder.getView(R.id.header_indicator);
                PagerAdapter pagerAdapter = new PagerAdapter() {
                    @Override
                    public int getCount() {
                        return imageViews.size();
                    }

                    @Override
                    public boolean isViewFromObject(View view, Object object) {
                        return view == object;
                    }

                    @Override
                    public Object instantiateItem(ViewGroup container, int position) {
                        container.addView(imageViews.get(position));
                        return imageViews.get(position);
                    }

                    @Override
                    public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView(imageViews.get(position));
                    }
                };
                viewPager.setAdapter(pagerAdapter);
                indicator.setupWithViewPager(viewPager);
                return holder;
            }

            @Override
            public void convert(ViewHolder holder, Object data) {

            }

        };
        return header;
    }

    @Override
    public boolean getmEnablePullLoad() {
        return true;
    }
}
